<?php
// +----------------------------------------------------------------------
// | 基础控制器
// +----------------------------------------------------------------------
namespace app\common\controller;
use Session;

class Admin extends Base {

    protected $userinfo;
    const session_scope = 'siteplus';  //SESSION作用域
    const session_name  = 'isAdmin';  //SESSION值

    public function _initialize() {
        session_start();
        $this->isLogin();
        $this->assign('app_name',lang('lang_appname'));
        $this->userinfo = Session::get(self::session_name,self::session_scope);
    } 

    /**
     * 权限验证
     * @access protected
     * @return bool
     */
    protected function isLogin(){
        //不需要登录验证的页面(注意大小写)
        $noLogin = [
            'admin' =>['Index'=>['login','logout']]
        ];
        //当前请求方法
        $module     = $this->request->module();
        $controller = $this->request->controller();
        $action     = $this->request->action();    
        if(isset($noLogin[$module])){
            if(isset($noLogin[$module][$controller]) && in_array($action, $noLogin[$module][$controller])){
                return TRUE;
            }
        }
        if(!Session::has(self::session_name,self::session_scope)){
            $this->setlogoutSession();
            $this->redirect(url('admin/index/login'),302);
        }
        return true;
    }


    /**
     * [isAuth 权限验证]
     * @param  [str]  $auth_code [权限识别码]
     * @param  [str]  $message   [无权限提示文字]
     * @return boolean           [是否有权限]
     */

    protected function isAuth($auth_code,$isAjax = FALSE,$message = '403 Forbidden'){
       $result = self::Access_Auth($auth_code);
       if(!$result){
            if($isAjax){
                exit(json_encode(['code'=>1,'message'=>$message]));
            }else{
                exit($this->success($message));
            }
       }
    }

    /**
     * [Access_Auth 权限验证]
     * @param  [str]  $auth_code [权限识别码]
     * @param  [str]  $message   [无权限提示文字]
     * @return boolean           [是否有权限]
     */
    private function Access_Auth($auth_code){
        //超级权限
        if($this->userinfo['group_id'] == 0){
            return true; 
        }
        $group = model('Admin')->find_group_info($this->userinfo['group_id']);
        if(!$group['auth_ids']){
            return FALSE; 
        }
        $auth_ids = json_decode($group['auth_ids'],TRUE);
        $list = model('Admin')->find_auth_select($auth_ids);
        if(!$list){
            return FALSE; 
        }
        foreach ($list as $value) {
            $auth_len =strlen($value['auth_code']);
            if(strncasecmp($auth_code,$value['auth_code'],$auth_len) == 0){
                return true; 
            }
        }
        return FALSE;
    }


    /**
     * 设置登录Session
     * @access public
     */
    protected function setLoginSession($param){
        $data = [
            'group_id' => $param['group_id'],
            'admin_id' => $param['id'],
            'login_time' => time(),
        ];
        Session::set(self::session_name,$data,self::session_scope);
    }

    /**
     * 退出Session
     * @access public
     */
    protected function setlogoutSession(){
        Session::delete(self::session_name,self::session_scope);
    }    
}
